package com.tongtech.example.mapper;

import org.mapstruct.Qualifier;
import org.springframework.stereotype.Component;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

// 自定义的转换器

@Component
public class StringToDateConverter {

    @Autowired
    SysBusiDictRepository sysBusiDictRepository;

    @Qualifier
    @Target(ElementType.METHOD)
    @Retention(RetentionPolicy.CLASS)
    public @interface StringToDate {}
    
    @Qualifier
    @Target(ElementType.METHOD)
    @Retention(RetentionPolicy.CLASS)
    public @interface DateToString {}

    @StringToDate
    public Date stringToDate(String date) {
        if (date == null) {
            return null;
        }
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return format.parse(date);
        } catch (ParseException e) {
            throw new RuntimeException("Date format error: " + date);
        }
    }

    @DateToString
    public String dateToString(Date date) {
        if (date == null) {
            return null;
        }
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(date);
    }

     // 查询字典
    public String queryDictName(String input, String fieldName) {
        SysBusiDict sysBusiDict = sysBusiDictRepository.findByDictCodeAndRemark(input, fieldName);
        if (sysBusiDict != null) return sysBusiDict.getDictName();
        return "未知";
    }
}
